package com.csdj.t301.util;

import org.aspectj.bridge.Message;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * author:cloud
 * date:2021/3/23 0023
 **/
public class EncryptUtil {

    public static String getMd5(String msg) {
        try {
            return getDigest(msg,"md5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getSha256(String msg) throws NoSuchAlgorithmException {
        return getDigest(msg,"SHA256");
    }

    private static String getDigest(String msg,String algorith) throws NoSuchAlgorithmException {
        StringBuilder sb = new StringBuilder();
        MessageDigest digest = MessageDigest.getInstance(algorith);
        digest.update(msg.getBytes());
        byte[] dig = digest.digest();
        for(int i=0;i<dig.length;i++){
            sb.append(String.format("%02x",dig[i]));
        }
        return sb.toString();
    }



    private static int recursive(int n){
        if(n == 1)return 1;
        return recursive(n-1)+n;
    }

    private static int fib(int n){
        if(n == 1 || n == 2)return 1;
        return fib(n-1)+fib(n-2);
    }





    public static void main(String[] args) {
        System.out.println(recursive(100));
        try {
            System.out.println(getSha256("123456"));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

}
